﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PingBao
{
    public partial class Form1 : Form
    {
        // ...
        private System.Threading.Timer _timer; // 修改类型为 System.Threading.Timer
        private NotifyIcon notifyIcon;

        public Form1()
        {
            InitializeComponent();

            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = SystemIcons.Application; // 可替换为自定义图标
            notifyIcon.Text = "PingBao 已最小化到托盘";
            notifyIcon.Visible = false;
            notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
        }

        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(out POINT lpPoint);

        private struct POINT
        {
            public int X;
            public int Y;
        }

        // ...
        private void btnStart_Click(object sender, EventArgs e)
        {
            int x = 10;
            try
            {
                x= int.Parse(textBox1.Text)*1000;
            }
            catch  
            {

                
            }
            // 启动定时器，每秒移动鼠标
            _timer = new System.Threading.Timer(MoveMouse, null, 0, x);
            btnStart.Enabled = false;
        }

        private void MoveMouse(object state)
        {
            if (GetCursorPos(out POINT currentPos))
            {
                // 移动鼠标 1 像素
                SetCursorPos(currentPos.X + 2, currentPos.Y);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            //停止定时器
            if (_timer != null)
            {
                _timer.Dispose();
                _timer = null;
            }
            btnStart.Enabled = true;
        }

        // 托盘图标双击事件，恢复窗口
        private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            this.Activate();
            notifyIcon.Visible = false;
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            // 隐藏窗口到托盘（任务栏不显示）
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            notifyIcon.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}